package com.ship.contract.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ship.common.core.base.Export;
import com.ship.common.core.base.Page;
import com.ship.contract.service.ExportService;
import com.ship.common.core.web.domain.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Collections;
import java.util.List;

/**
 * @author 31463
 */
@RestController
@RequestMapping("/export")
public class ExportController {

    @Autowired
    private ExportService exportService;

    /**
     * 分页
     *
     * @param export
     * @return
     */
    @PostMapping("/page")
    public JsonResult<Page<Export>> page(@RequestBody Export export) {
        Page<Export> page = exportService.page(export,new QueryWrapper<>());
        List<Export> exportList = page.getList();

        // 对列表按时间倒序排序
        Collections.sort(exportList, (e1, e2) -> e2.getCreateDate().compareTo(e1.getCreateDate()));

        long pageSize = page.getPageSize();
        long currentPage = page.getPageNum();
        int startOrderNumber = (int) ((currentPage - 1) * pageSize + 1);

        // 为每个Export对象设置orderNumber
        for (int i = 0; i < exportList.size(); i++) {
            exportList.get(i).setOrderNumber(startOrderNumber + i);
        }

        page.setList(exportList);
        return JsonResult.success(page);
    }

}
